package org.soc.timesheet.server.domain.service;

import java.util.List;
import org.soc.timesheet.domain.Projects;
import org.soc.timesheet.domain.SubProjects;
import org.soc.timesheet.domain.repository.SubProjectsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SubProjectsServiceImpl implements SubProjectsService {
	
	@Override
	public Page<SubProjects> findByProject(Projects project,
			PageRequest pageRequest) {
		// TODO Auto-generated method stub
		return subProjectsRepository.findByIdProject(project,pageRequest);
	}


	@Override
	public Page<SubProjects> findAll(PageRequest pageRequest) {
		// TODO Auto-generated method stub
		return subProjectsRepository.findAll(pageRequest);
	}


	@Autowired
	TimesheetService timesheetService;
	
	@Autowired
	UsersProjectService usersProjectService;
	
	
	public List<SubProjects> findByProject(Projects project){
		return subProjectsRepository.findByIdProject(project);
	}


	public void deleteSubProjects(SubProjects subProjects) {
		timesheetService.deleteBySubproject(subProjects.getId());
		usersProjectService.deleteBySubProject(subProjects.getId());
		
        subProjectsRepository.delete(subProjects); 
    }

	@Autowired
    SubProjectsRepository subProjectsRepository;

	public long countAllSubProjectses() {
        return subProjectsRepository.count();
    }

	public SubProjects findSubProjects(Integer id) {
        return subProjectsRepository.findOne(id);
    }

	public List<SubProjects> findAllSubProjectses() {
        return subProjectsRepository.findAll();
    }

	public List<SubProjects> findSubProjectsEntries(int firstResult, int maxResults) {
        return subProjectsRepository.findAll(new org.springframework.data.domain.PageRequest(firstResult / maxResults, maxResults)).getContent();
    }

	public void saveSubProjects(SubProjects subProjects) {
        subProjectsRepository.save(subProjects);
    }

	public SubProjects updateSubProjects(SubProjects subProjects) {
        return subProjectsRepository.save(subProjects);
    }
}
